package main

import (
	"os"

	"gitee.com/Frozentear/layout/internal/conf"
	"gitee.com/bethink1501/24on-library/zlog"
	"github.com/go-kratos/kratos/v2"
	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware/tracing"
	"github.com/go-kratos/kratos/v2/registry"
	"github.com/go-kratos/kratos/v2/transport/grpc"
	"github.com/go-kratos/kratos/v2/transport/http"
)

var (
	Name    string = "layout"
	Version string = "0.0.1"
	id, _          = os.Hostname()
)

// @title example API
// @version 1.0
// @description example 主页接口.
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
// @BasePath /layout
func main() {
	config := conf.InitConfig()
	zlog.InitLogger(config.Log)

	logger := log.With(log.NewStdLogger(os.Stdout),
		"ts", log.DefaultTimestamp,
		"caller", log.DefaultCaller,
		"service.id", id,
		"service.name", Name,
		"service.version", Version,
		"trace.id", tracing.TraceID(),
		"span.id", tracing.SpanID(),
	)

	app, call, err := initApp(logger, config.Server, config.Data, config.Registry)
	if err != nil {
		panic(err)
	}
	defer call()

	if err := app.Run(); err != nil {
		panic(err)
	}
}

func newApp(logger log.Logger, hs *http.Server, gs *grpc.Server, rs registry.Registrar) *kratos.App {
	return kratos.New(
		kratos.Name(Name),
		kratos.Logger(logger),
		kratos.Version(Version),
		kratos.Registrar(rs),
		kratos.Server(hs,gs),
	)

}
